// pages/operators/goodsAdd/goodsAdd.js
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    tag: '',
    tags: [],
    data: null,
    vInfo: null,
    files: [],
    img: null,
    editor: null,
    detail: null,
    show: false,
    isAdd: false,
    productList: null,
    // selectFile: function (e) {
    //   console.log(e)
    // },
    uplaodFile: function (e) {
      let self = this;
      return new Promise(function (resolve, reject) {
        // console.log(123, e)
        for (let url of e.tempFilePaths) {
          wx.uploadFile({
            url: api.StorageUpload,
            name: 'file',
            filePath: url,
            success: function (e) {
              let file = JSON.parse(e.data)
              resolve({
                urls: [file.data.url]
              })
              // resolve({
              //   urls: e.tempFilePaths
              // })
            }
          })
        }
      })
    },
  },
  uploadError: function (e) {
    // console.log(e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  showTag() {
    this.setData({
      show: true
    })
  },
  ok() {
    this.setData({
      show: false
    })
  },
  tagChange(e) {
    // console.log(22, e)
    this.setData({

      tag: e.detail.value
    })
  },
  delTag(e) {
    // console.log(1, e)
    let {
      id
    } = e.target.dataset;
    let tags = this.data.tags;
    tags.splice(id, 1);
    // console.log(id, tags)
    this.setData({
      tags
    })
  },
  detailChange(e) {
    // console.log(222, e.detail.html)
    this.setData({
      detail: e.detail.html
    })
  },
  addTag() {
    // console.log(33)
    this.setData({
      isAdd: true
    })
  },
  createTag() {
    // console.log(32, e)
    let tags = this.data.tags
    let isAdd = false
    // console.log(32, this)
    if (this.data.tag) {
      tags.push(this.data.tag)
    }
    this.setData({
      isAdd,
      tags,
      tag: ''
    })
  },
  getVendor() {
    let self = this;
    wx.getStorage({
      key: 'vInfo',
      success: function (res) {
        let vInfo = res.data;
        self.setData({
          vInfo
        });
        // self.getOrderList()
      },
    })
  },
  uploadSuccess: function (e) {
    // console.log(123, this.data, e.detail.urls[0])
    // let data = this.data.data;
    // data.picUrl = e.detail.urls[0];
    this.setData({
      img: e.detail.urls[0]
    });
  },
  onLoad: function (options) {
    const self = this;
    let editor = null;
    wx.createSelectorQuery().select('#editor').context(res => {
      editor = res.context;
      if (options.id) {
        util.request(api.GoodsDetail, {
          id: options.id
        }).then(r => {
          // console.log(123, editor)
          this.setData({
            id: options.id,
            data: r.data.info,
            fl: r.data.info.categoryId,
            // productList: r.data.productList,
            specificationList: r.data.productList.map(r => {
              return r.specifications.join(',')
            }),
            files: [{
              url: r.data.info.picUrl
            }]
          })
          editor.setContents({
            html: r.data.info.detail
          })
        })
      }
    }).exec();
    this.getVendor()
    this.getTags()
    this.getType()
  },
  getType() {
    var self = this;
    util.request(api.CatalogList).then(r => {
      // console.log(23, r)
      self.setData({
        productList: r.data.categoryList
      })
    })
  },
  catChange: function (e) {
    var data = this.data.data || {};
    data.categoryId = this.data.productList[e.detail.value * 1].id
    // console.log(data.categoryId, e)
    this.setData({
      data,
      fl: e.detail.value
    })
  },
  getTags: function () {
    wx.request({
      url: api.listByTag,
      data: {
        limit: 1000,
        page: 1,
      },
      success(r) {
        // console.log(222, r)
      }
    })
  },

  save: function (e) {
    var dao = api.goodAdd
    let goods = e.detail.value;
    goods.goodsSn = Math.random();
    goods.detail = this.data.detail
    if (this.data.id) {
      dao = api.goodEdit
      goods.id = this.data.id;
    }
    if (this.data.img) {
      goods.picUrl = this.data.img;
    }
    goods.categoryId = this.data.categoryId;
    let tags;
    if (this.data.tags) {
      tags = this.data.tags.map(r => {
        return {
          name: r
        }
      })
    }
    let vendorId = this.data.vInfo.id;
    wx.request({
      url: dao,
      data: {
        goods,
        tags,
        vendorId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success(re) {
        if (re.data.errno) {
          wx.showToast({
            icon: 'none',
            title: re.data.errmsg,
          })
        } else {
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})